package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.databinding.DialogMemberCustomerServiceItemBinding;
import com.sgb.member.entity.MemberCompanyHeaderBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.lxj.xpopup.core.CenterPopupView;

@SuppressLint("ViewConstructor")
public class MemberExchangePopup extends CenterPopupView {

    private int mCurIndex = 0;
    private final MemberCompanyHeaderBean mHeaderBean;
    public MemberExchangePopup(@NonNull Context context, MemberCompanyHeaderBean headerBean, OnConfirmListener confirmListener) {
        super(context);
        mHeaderBean = headerBean;
        setConfirmListener(confirmListener);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_member_customer_service;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        findViewById(R.id.iv_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView resetView = findViewById(R.id.tv_title);
        resetView.setText(mHeaderBean.companyName);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new BaseRecycleAdapter<MemberCompanyHeaderBean.CustomerListBean>(getContext(),
                R.layout.dialog_member_customer_service_item, mHeaderBean.customerList) {
            @Override
            public void convert(ItemViewHolder holder, MemberCompanyHeaderBean.CustomerListBean o) {
                holder.setBinding(BR.viewModel, o.principalName);
            }

            @Override
            public void onBindViewHolder(ItemViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                DialogMemberCustomerServiceItemBinding binding = (DialogMemberCustomerServiceItemBinding) holder.getmBinding();
                if (mCurIndex == position) {
                    binding.kfLayout.setBackgroundResource(R.drawable.member_shape_rectangle_stroke_5792fd);
                    binding.ivKf.setImageResource(R.drawable.member_icon_kf_check);
                } else {
                    binding.kfLayout.setBackgroundResource(R.drawable.member_shape_rectangle_stroke_c0c4cc);
                    binding.ivKf.setImageResource(R.drawable.member_icon_kf);
                }
                binding.kfLayout.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCurIndex = position;
                        if(confirmListener != null){
                            confirmListener.onConfirm(mHeaderBean.customerList.get(position));
                        }
                    }
                });
            }
        });
    }

    private OnConfirmListener confirmListener;

    public void setConfirmListener(OnConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public interface OnConfirmListener {
        void onConfirm(MemberCompanyHeaderBean.CustomerListBean code);
    }
}
